package com.example.lotterysystem.service.activityStatus.impl;

import com.example.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.example.lotterysystem.common.exception.ServiceException;
import com.example.lotterysystem.service.IActivityService;
import com.example.lotterysystem.service.activityStatus.IActivityStatusManager;
import com.example.lotterysystem.service.activityStatus.operator.AbstractActivityOperator;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Slf4j
@Component
public class ActivityStatusManagerImpl implements IActivityStatusManager {
    @Autowired
    private final Map<String, AbstractActivityOperator> operatorMap = new HashMap<>();
    @Autowired
    private IActivityService activityService;


    @Override
    public void handlerEvent(ConvertActivityStatusDTO convertActivityStatusDTO) {
        if (CollectionUtils.isEmpty(operatorMap)) {
            log.warn("operatorMap 为空");
            return;
        }
        Map<String, AbstractActivityOperator> currMap = new HashMap<>(operatorMap);

        Boolean update = false;
        //先处理奖品, 人员
        update = processConvertStatus(convertActivityStatusDTO, currMap, 1);
        //后处理活动
        update = processConvertStatus(convertActivityStatusDTO, currMap, 2) || update;
        //更新缓存
        if (update) {
            activityService.cacheActivity(convertActivityStatusDTO.getActivityId());
        }
    }

    @Override
    public void rollbackHandlerEvent(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //operatorMap: 活动, 奖品, 人员, 没有顺序
        for(AbstractActivityOperator operator : operatorMap.values()){
            operator.convert(convertActivityStatusDTO);
        }
        //缓存更新
        activityService.cacheActivity(convertActivityStatusDTO.getActivityId());
    }

    private Boolean processConvertStatus(ConvertActivityStatusDTO convertActivityStatusDTO,
                                         Map<String, AbstractActivityOperator> currMap,
                                         int sequence) {
        Boolean update = false;
        //遍历map
        Iterator<Map.Entry<String, AbstractActivityOperator>> iterator = currMap.entrySet().iterator();
        while (iterator.hasNext()) {
            AbstractActivityOperator operator = iterator.next().getValue();
            //operator是否需要转换
            if (operator.sequence() != sequence
                    || !operator.needConvert(convertActivityStatusDTO)) {
                continue;
            }
            //需要转换
            if(!operator.convert(convertActivityStatusDTO)){
                log.error("{}转换失败", operator.getClass().getName());
                throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_CONVERT_ERROR);
            }
            //currMap 删除当前Operator
            iterator.remove();
            update = true;
        }
        return update;
    }
}
